jQuery.fn.rotate=function(angle,topValue,leftValue,widthScale,heightScale,whence) {
	var p = this.get(0);

	// we store the angle inside the image tag for persistence
	if (!whence) {
		p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;
	} else {
		p.angle = angle;
	}

	if (p.angle >= 0) {
		var rotation = Math.PI * p.angle / 180;
	} else {
		var rotation = Math.PI * (360+p.angle) / 180;
	}
	var costheta = Math.cos(rotation);
	var sintheta = Math.sin(rotation);

	if (document.all && !window.opera) {
		var canvas = document.createElement('img');

		canvas.src = p.src;
		canvas.height = p.height*heightScale;
		canvas.width = p.width*widthScale;

		canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
	} else {
		var canvas = document.createElement('canvas');
		if (!p.oImage) {
			canvas.oImage = new Image();
			canvas.oImage.src = p.src;
		} else {
			canvas.oImage = p.oImage;
		}
		
		canvas.oImage.width*=widthScale;
		canvas.oImage.height*=heightScale;
		//alert(canvas.oImage.height);

		canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);
		canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);

		var context = canvas.getContext('2d');
		context.save();
		if (rotation <= Math.PI/2) {
			context.translate(sintheta*canvas.oImage.height,0);
		} else if (rotation <= Math.PI) {
			context.translate(canvas.width,-costheta*canvas.oImage.height);
		} else if (rotation <= 1.5*Math.PI) {
			context.translate(-costheta*canvas.oImage.width,canvas.height);
		} else {
			context.translate(0,-sintheta*canvas.oImage.width);
		}
		context.rotate(rotation);
		context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
		context.restore();
	}
	canvas.id = p.id;
	canvas.angle = p.angle;
	canvas.style.position = 'absolute';
	canvas.style.top=topValue+"px";
	canvas.style.left=leftValue+"px";
	
	p.parentNode.replaceChild(canvas, p);

}

jQuery.fn.rotateLeft=function(angle,topValue,leftValue,widthScale, heightScale) {
	this.rotate(angle==undefined?-90:-angle,topValue,leftValue,widthScale, heightScale);
}

// Charles add
function myThrow() {	
	var widthValue=$('#throwTest').width();
	var heightValue=$('#throwTest').height();
	var topValue=$('#throwTest').position().top;
	var leftValue=$('#throwTest').position().left;
	
	$('#throwTest').rotateLeft(30, topValue, leftValue, 0.9, 0.9);
	if (interval < 10) {
		interval++;
		topValue=topValue-5;
  } else if (interval > 20) {
  	interval=0;
  	$('#throwTest').css('display','none');
  	throwTrigger.disabled=false;
  	return;
    // TODO
  } else {
  	interval++;
  	topValue=topValue+10;
  }
  
  $('#throwTest').css('z-index',15).animate({
		  top:topValue
    },25,myThrow);
}

var interval=0;
var throwTrigger;
var salvageTrigger;

function throwTestFn(origObj, bottleSrc) {
	throwTrigger = origObj;
	throwTrigger.disabled=true;
	resetThrownBottle(bottleSrc);
	myThrow(origObj);
}

function resetThrownBottle(bottleSrc) {
	var p = $('#throwTest').get(0);
	var canvas = document.createElement('img');
	canvas.id = p.id;
	canvas.src=bottleSrc;
	p.parentNode.replaceChild(canvas, p);
  $('#throwTest').css('position','absolute').css('z-index',15).css({
	  width:173,
	  height:224,
	  top:'20%',
	  left:'50%'
  });
}

function wrecking(origObj, bottleSrc){
	resetBothSalvageBottle(bottleSrc);
		
	salvageTrigger = origObj;
	salvageTrigger.disabled=true;
	
  var leftValue=$('#salvage').position().left+100;
  $('#salvage').animate({
  	width:150,
  	height:96,
  	left:leftValue
	  },'fast',salvageMoveTop);
}

function salvageMoveTop(){
	var topValue=$(this).position().top;
	var leftValue=$(this).position().left;

  if (document.all && !window.opera) {
		$('#salvage').rotateLeft(45, topValue,leftValue,1,1);
	} else {
	  $('#salvage').rotateLeft(45, topValue,leftValue,0.5,0.5);
	}
	
  $('#salvage').css('z-index',999).animate({
	  top:-100
	},'slow',salvageMoveDig);
}

function salvageMoveDig(){
	$('#salvage').css('z-index',3).animate({
	  top:248-$(this).height(),
	  left:$(window).width()*0.5
	},'fast',salvageForth);
}

function salvageForth(){
		$('#salvage').animate({
	    left:$(window).width()*0.4
	  },'slow',salvageBack);
}

function salvageBack(){
		$('#salvage').animate({
	    left:$(window).width()*0.5
	  },'slow',salvageUpWithBottle);
}

function salvageUp(){
  $('#salvage').animate({
	    top:$('#salvage').position().top-$('#salvage').height()*0.5
	  },'slow',salvageNear);
}

function salvageUpWithBottle() {
	var iTop=$('#salvage').position().top+$('#salvage').height()/3;
	var iLeft=$('#salvage').position().left+5;
  var iWidth=$('#salvage').width()/2.2;
	var iHeight=iWidth*1.345;
	var iUp=$('#salvage').height()/2;
	
	bottleSet(iTop,iLeft,iWidth,iHeight);
	
	salvageUp();
	bottleUp(iUp);
}

function bottleSet(iTop,iLeft,iWidth,iHeight) {
	$('#bottle').css('position','absolute').css('z-index',2).css({
		top:iTop,
		left:iLeft,
		width:iWidth,
		height:iHeight
	});
}

function bottleUp(iHeight) {
	$('#bottle').css('z-index',2).animate({
		top:$('#bottle').position().top-iHeight
	},'slow',bottleNear);
}

function salvageNear() {
	var topValue=$(this).position().top;
	var leftValue=$(this).position().left;
	//var widthValue=$(this).width()*1.6;
	var heightValue=$(this).height();
	$('#salvage').rotateLeft(-45,topValue,leftValue,1,1);
	
	$('#salvage').css('z-index', 999).animate({
	    width:225,
	    height:145,
	    top:topValue+heightValue
	  },'slow');
}

function bottleNear() {
	var topValue=$(this).position().top;
	var leftValue=$(this).position().left;

	var iTop=(document.all && !window.opera)?$('#salvage').position().top-25:$('#salvage').position().top-20;
	var iLeft=(document.all && !window.opera)?$('#salvage').position().left-27:$('#salvage').position().left-20;
  var iWidth=$('#bottle').width()/1.6;
	var iHeight=iWidth*1.345;
	bottleSet(iTop,iLeft,iWidth,iHeight);
	
	heightValue=$(this).height();
	
		$('#bottle').css('position','absolute').css('z-index', 998).animate({
	    width:134,
	    height:180,
	    top:topValue+heightValue
	  },'slow', enableSalvageTrigger);
}

function enableSalvageTrigger() {
  salvageTrigger.disabled=false;
}

function resetBothSalvageBottle(bottleSrc) {
  var p = $('#salvage').get(0);
	var canvas = document.createElement('img');
	canvas.id = p.id;
	canvas.src=bottleSrc;
	p.parentNode.replaceChild(canvas, p);
  $('#salvage').css('position','absolute').css('z-index',999).css({
	  width:0,
	  height:0,
	  top:'80%',
	  left:'60%'
  });
  
  $('#bottle').attr("src","images/bottle/salvage2[1].png").css('position','absolute').css('z-index',1).css({
	  width:0,
	  height:0
  });
}